Skip to content

Omit empty text from message content parts - #699

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/empty-text-content-part
Oct 15, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
aleroot:alessio/empty-text-content-part

Conversation

@aleroot

@aleroot aleroot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Proposed changes

contentParts(for:layout:) always appends a text part. An assistant tool call without text becomes [{"type": "text", "text": ""}]. Chat templates test message.content for truth: an empty string is false, but a list that holds
an empty part is true.

On Qwen3-VL this breaks KV cache reuse on every tool call:

  1. The template renders the call as \n\n<tool_call>. The model generated \n<tool_call>.
  2. The re-rendered prompt no longer extends the cached tokens, so the session must rewind.
  3. RewindToCommonPrefixRule refuses the rewind because the model carries M-RoPE state, so the decision is .rebuild.

So each tool follow-up pass prefills the whole conversation again.

This change omits empty text: it gives no part. An empty list is false, as an empty string is, so the re-rendered prompt extends the cache again.

Qwen3-VL-2B-Instruct-4bit Before After
Positions reused by the pass after a tool call 0 of 297 297 of 297
Positions reused by the next user turn 369 361 of 375

The next user turn reused its prefix before the fix too; the fix leaves it
working. The two runs generate different replies, so the counts differ.

Notes

  • Models checked. I rendered the chat template of every registered model whose message generator uses content parts: Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3.5, Qwen3.8, Muse-Glimmer, Gemma 3, Gemma 4, LFM2-VL, Ministral 3, FastVLM and GLM-OCR. Each was rendered with an empty text part and with no part, for tool-call turns, empty replies and image-only user turns.
    • Only the Qwen3-VL template changes output. It now renders the same as with string content.
    • In every other template, an empty text part and no part give the same render.
  • Behaviour change. On Ministral 3, an assistant message with no text and no tool calls now raises the template's own error. The template rejects such a message, and it rejects an empty string content the same way. The empty part only passed that check by accident.
  • Generators not changed. SmolVLM2MessageGenerator builds its own content and does not use contentParts. Its template reads message['content'][0], so it must always get a part.
  • Test replaced. "Empty text still gives a text part" only pinned the pre-label array shape. It is replaced by "Empty text gives no part". A new test checks that a tool call without text gets empty content from every generator that uses content parts. It fails without the fix.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure:

`contentParts(for:layout:)` always appended a text part, so an assistant
tool call without text became `[{"type": "text", "text": ""}]`. Chat
templates test `message.content` for truth: the empty string a text
model receives is false, but a list holding an empty part is true.

Qwen3-VL's template then renders the call as `\n\n<tool_call>` instead
of the `\n<tool_call>` the model generated. The cache no longer matches
the prompt, and a rewind is refused because the model carries M-RoPE
state, so every tool follow-up pass prefilled the whole conversation.

Empty text now gives no part. An empty list is false, as the empty
string is, so the re-rendered prompt extends the cache again.

Checked against the chat templates of every registered model that uses
content parts (Qwen2-VL, Qwen2.5-VL, Qwen3-VL, Qwen3.5, Qwen3.8,
Muse-Glimmer, Gemma 3, Gemma 4, LFM2-VL, Ministral 3, FastVLM, GLM-OCR):
only the Qwen3-VL template changes output, and it now renders the same
as with string content.
Comment thread Libraries/MLXLMCommon/Chat.swift Outdated
var parts: [[String: String]] = []

func appendText(_ text: String) {
guard !text.isEmpty else { return }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't safe for all models. Consider:

https://huggingface.co/mlx-community/Ministral-3-3B-Instruct-2512-4bit/blob/main/chat_template.jinja

    {#- User messages supports text content or text and image chunks. #}
    {%- if message['role'] == 'user' %}
        {%- if message['content'] is string %}
            {{- '[INST]' + message['content'] + '[/INST]' }}
        {%- elif message['content'] | length > 0 %}
...
        {%- else %}
            {{- raise_exception('User message must have a string or a list of chunks in content') }}
        {%- endif %}

Though curiously the upstream template differs:

https://huggingface.co/mistralai/Ministral-3-3B-Instruct-2512/blob/main/chat_template.jinja

There is a similar rule for assistant messages:

    {#- Assistant messages supports text content. #}
    {%- elif message['role'] == 'assistant' %}
        {%- if (message['content'] is none or message['content'] == '' or message['content']|length == 0) and (message['tool_calls'] is not defined or message['tool_calls'] is none or message['tool_calls']|length == 0) %}
            {{- raise_exception('Assistant message must have a string or a list of chunks in content or a list of tool calls.') }}

but nothing for user messages -- it looks like a lot has changed in the template since the mlx quantization was made.

Anyway, some possibilities:

  • make this be opt-in or opt-out, Mistral3MessageGenerator can implement
    • this is just one I (claude really) found as being affected, so it make require a bunch of tests to make sure we have good coverage
  • only omit when the result wouldn't be empty -- might require looking at the layout or post-filtering
  • the PR was concerned about tool calls, so maybe limit it to that?

The seems to be an arbitrary rule on the mistral side, so none of these stand out as best, but maybe item 3 is the narrowest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I’ve limited the omission to assistant messages with nonempty tool-call lists, as suggested. Other empty messages keep their text part.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but see my comment/question on mistral3. Thanks!

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good, thank you!

@davidkoski
davidkoski merged commit 8ec6a2c into ml-explore:main Oct 15, 2026
5 checks passed
ximyro pushed a commit to ximyro/mlx-swift-lm that referenced this pull request Nov 4, 2026
Brings in ml-explore#622 (exact prefix rewinds in rotating KV caches), ml-explore#619 (streaming
detokenizers), ml-explore#698, ml-explore#699, ml-explore#700, ml-explore#701, ml-explore#705, ml-explore#697 and ml-explore#695.

Conflicts: AttentionUtils.swift keeps TurboKV telemetry next to upstream's
SharedAttentionKVCache; KVCache.swift takes upstream's serialization and
rotating-cache view/trim while keeping the fork's TurboQuant state, public
cache fields and temporallyOrdered.

Follow-ups for upstream behavior the fork had changed: the Qwen2VL audio
placeholder no longer indexes content.count - 1 when a tool-call message has no
text part, and Gemma4MessageGenerator uses content parts for every non-system
role.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants